const { setAge } = require("../../../../utils/api");

// pages/signinpage/component/rule-dialog/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    minDate: new Date(1920, 1, 1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      }
      if (type === 'month') {
        return `${value}`;
      }
      return value;
    },

  },

  /**
   * 组件的方法列表
   */
  methods: {
    colseAge() {
      this.setData({
        show: false
      })
    },
    onChange(event) {
      let temp = event.detail.getValues();

      const ttr = temp[0] + "-" + temp[1] + "-" + temp[2];

      this.setData({
        currentDate: ttr,
      });
    },


    settingAge() {
      let maxDateObj = new Date(this.data.maxDate)
      let currentDateObj = new Date(this.data.currentDate)
      var age = maxDateObj.getFullYear() - currentDateObj.getFullYear();
      if (maxDateObj.getMonth() < currentDateObj.getMonth() || (maxDateObj.getMonth() == currentDateObj.getMonth() && maxDateObj.getDate() < currentDateObj.getDate())) {
        age--;
      }

      let params = {
        age: age
      }
      setAge(params).then((res) => {
        let that = this

        if (res.errcode == 0) {
          wx.showToast({
            title: '年龄已设置',
            icon: 'success',

            success: (result) => {
              that.setData({
                show: false
              })
              setTimeout(function () { that.triggerEvent("parentFun"); }, 1000);
            },

          });


        } else {
          wx.showToast({
            title: res.errmsg,
            icon: 'none',
          });

        }
      })

    }



  }
})
